Skip to content

Conversation

bennysj
Copy link
Contributor

@bennysj bennysj commented Sep 16, 2025

The idea to make it easier and more convenient to pass a reference to a Slint global instance into a Rust closure / lambda.

The current implementation works the following way:

  pub fn init(blogica_api: &BLogicAAPI) {
        blogica_api.on_update_data({
            let blogica_api_weak = blogica_api.as_weak();
            |bdata| {
                let blogica_api = blogica_api_weak.upgrade().unwrap();
                // It would have been very nice if the call to .global() could avoided, as it depends on the lifetime
                // of the GlobalStrong<T> it's needed
                let blogica_api = blogica_api_weak.to_global();

                // Do stuff here with blogica_api
            }
        });
    }

Fixes #9389

@bennysj bennysj force-pushed the global_comp_improvments branch 4 times, most recently from ab2a50a to 595f2e2 Compare September 16, 2025 14:06
pub use weak_handle::*;

/// This trait provides the necessary functionality for allowing creating strongly-referenced
/// clones and conversion into/ a weak pointer for a Global slint component.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

into/ ?

Did you mean into/from maybe, or is the / a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

/// Returns a new GlobalStrong struct, where it's possible to get the global component
/// struct interface. Ff some other instance still
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ff => If

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if std::thread::current().id() != self.thread {
return None;
}
let coso = T::upgrade_from_weak_inner(&self.inner)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coso?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to 'inner'

/// // now forward the data to the main thread using upgrade_in_event_loop
/// my_app_data_weak.upgrade_in_event_loop(move |my_app_data| my_app_data.set_foo(foo));
/// });
/// # thread.join().unwrap(); return; // don't run the event loop in examples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. This is docu, not actual example, why the commented-out code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a lot of copy / paste from the same function for Weak - at line 930

@bennysj bennysj force-pushed the global_comp_improvments branch 3 times, most recently from d2882a0 to 91264dc Compare September 25, 2025 18:47
The idea to make it easier and more convenient to pass a reference
to a Slint global instance into a Rust closure / lambda.

Fixes slint-ui#9389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement .as_weak() and .upgrade() for global component instances
2 participants